package shared.implicits

import scala.collection.immutable.SeqMap

/**
 * Created by Administrator on 2019/9/12
 */
trait MyMapTool {

  implicit class MyMapKT[K](map: collection.Map[K, String]) {

    def keyList = {
      map.keys.toList
    }

    def reverses = {
      map.groupBy(_._2).mapValues(x => x.keyList)
    }

    def valueList = {
      map.values.toList
    }

  }

  implicit class MyMap(map: collection.Map[String, String]) {

    def getOrElseIgnoreCase[V1 >: String](key: String, default: => V1) = {
      map.map { case (k, v) =>
        (k.toLowerCase, v)
      }.getOrElse(key.toLowerCase, default)
    }

    def valueList = {
      map.values.toList
    }

  }

  implicit class MySeqMap(map: SeqMap[String, String]) {

    def getIgnoreCaseOrElse(key: String, default: String) = {
      map.map { case (k, v) =>
        (k.toLowerCase, v)
      }.getOrElse(key.toLowerCase, default)
    }

    def keyList = {
      map.keys.toList
    }

  }

  implicit class MyMapVT[B](map: Map[String, B]) {

    def reverse = {
      map.map { case (k, v) =>
        (v -> k)
      }
    }

    def keyList = {
      map.keys.toList
    }

    def containsIgnoreCase(key: String) = {
      map.map { case (k, v) =>
        (k.toLowerCase, v)
      }.contains(key.toLowerCase)
    }

  }


}
